// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/chromeos/login/hardware_data_collection_screen_handler.h"

#include <string>

#include "chrome/browser/ash/login/oobe_screen.h"
#include "chrome/browser/ash/login/screens/hardware_data_collection_screen.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/login/localized_values_builder.h"

namespace chromeos {

constexpr StaticOobeScreenId HWDataCollectionView::kScreenId;

HWDataCollectionScreenHandler::HWDataCollectionScreenHandler(
    JSCallsContainer* js_calls_container)
    : BaseScreenHandler(kScreenId, js_calls_container) {
  set_user_acted_method_path("login.HWDataCollectionScreen.userActed");
}

HWDataCollectionScreenHandler::~HWDataCollectionScreenHandler() {
  if (screen_)
    screen_->OnViewDestroyed(this);
}

void HWDataCollectionScreenHandler::Show() {
  if (!page_is_ready()) {
    show_on_init_ = true;
    return;
  }
  ShowScreen(kScreenId);
}

void HWDataCollectionScreenHandler::Hide() {
  show_on_init_ = false;
}

void HWDataCollectionScreenHandler::Bind(ash::HWDataCollectionScreen* screen) {
  screen_ = screen;
  BaseScreenHandler::SetBaseScreen(screen_);
}

void HWDataCollectionScreenHandler::Unbind() {
  screen_ = nullptr;
  BaseScreenHandler::SetBaseScreen(nullptr);
}

void HWDataCollectionScreenHandler::ShowHWDataUsageLearnMore() {
  // TODO(dkuzmin): add help article here b/190964241
}

void HWDataCollectionScreenHandler::DeclareLocalizedValues(
    ::login::LocalizedValuesBuilder* builder) {
  builder->AddF("HWDataCollectionTitle", IDS_HW_DATA_COLLECTION_TITLE,
                IDS_INSTALLED_PRODUCT_OS_NAME);
  builder->AddF("HWDataCollectionContent", IDS_HW_DATA_COLLECTION_CONTENT,
                IDS_INSTALLED_PRODUCT_OS_NAME);
  builder->AddF("HWDataCollectionUsageInfo", IDS_HW_DATA_COLLECTION_USAGE_INFO,
                IDS_INSTALLED_PRODUCT_OS_NAME);
  builder->Add("HWDataCollectionNextButton",
               IDS_HW_DATA_COLLECTION_ACCEPT_AND_CONTINUE_BUTTON_TEXT);
}

void HWDataCollectionScreenHandler::Initialize() {
  if (!page_is_ready() || !screen_)
    return;

  CallJS("login.HWDataCollectionScreen.setHWDataUsage",
         screen_->IsHWDataUsageEnabled());

  if (show_on_init_) {
    Show();
    show_on_init_ = false;
  }
}

}  // namespace chromeos
